package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;
import com.widget.SwipeMenuView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InvitationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context = null;
    private List<PenpalArticleResponse.ResponseBean> invitationList = null;
    //interface
    private OnItemFunctionClickListener mOnItemFunctionClickListener = null;

    public InvitationAdapter(Context context) {
        this.context = context;
    }

    public void setInvitations(List<PenpalArticleResponse.ResponseBean> list) {
        this.invitationList = list;
    }

    public void addInvitations(List<PenpalArticleResponse.ResponseBean> list) {
        this.invitationList.addAll(list);
    }

    public List<PenpalArticleResponse.ResponseBean> getInvitations() {
        return this.invitationList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_invitation_item_view, parent, false);
        return new InvitationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        PenpalArticleResponse.ResponseBean invitation = invitationList.get(position);
        final InvitationViewHolder holder = (InvitationViewHolder) viewHolder;
        holder.articleImg.setVisibility(TextUtils.isEmpty(invitation.getTopImg()) ? View.GONE : View.VISIBLE);
        GlideUtils.loadImageView(context, holder.articleImg, invitation.getTopImg(),
                R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        holder.articleTitle.setVisibility(TextUtils.isEmpty(invitation.getTitle()) ? View.GONE : View.VISIBLE);
        holder.articleTitle.setText(invitation.getTitle());
        try {
            String content = invitation.getContents_header().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            holder.articleContent.setText(URLDecoder.decode(content, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.creatDate.setText(DateUtils.ConvertDate(invitation.getCreate_datetime()));
        holder.praseNum.setText("点赞" + invitation.getLikeCount());
        holder.commentNum.setText("评论" + invitation.getCommentCount());
        holder.main_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemFunctionClickListener != null) {
                    mOnItemFunctionClickListener.onClick(position);
                }
            }
        });

        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemFunctionClickListener != null) {
                    mOnItemFunctionClickListener.onDeleteClick(position);
                    holder.swipemenu_view.quickClose();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return invitationList == null ? 0 : invitationList.size();
    }

    public static class InvitationViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.main_layout)
        LinearLayout main_layout;
        @BindView(R.id.article_img)
        ImageView articleImg;
        @BindView(R.id.article_title)
        TextView articleTitle;
        @BindView(R.id.article_content)
        TextView articleContent;
        @BindView(R.id.creat_date)
        TextView creatDate;
        @BindView(R.id.prase_num)
        TextView praseNum;
        @BindView(R.id.comment_num)
        TextView commentNum;
        @BindView(R.id.swipemenu_view)
        SwipeMenuView swipemenu_view;
        @BindView(R.id.btn_delete)
        TextView btn_delete;

        public InvitationViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemFunctionClickListener(OnItemFunctionClickListener listener) {
        mOnItemFunctionClickListener = listener;
    }

    //interface
    public interface OnItemFunctionClickListener {

        void onClick(int position);

        void onDeleteClick(int position);
    }
}
